import { UserInfo } from '@/store/user/types'
const LOCAL_TOKEN = 'token'
const USER_INFO = 'userInfo'
const LOGIN_STATUS = 'isLogin'
const NEWS_INFO = 'newsInfo'
const SYSTEM_CONFIG = 'systemConfig'
export const getLocalToken = (): string | undefined => {
  return localStorage.getItem(LOCAL_TOKEN) || undefined
}
export const setLocalToken = (token: string): void => {
  localStorage.setItem(LOCAL_TOKEN, token)
}
export const clearLocalToken = (): void => {
  localStorage.removeItem(LOCAL_TOKEN)
}

export const getLocalUserInfo = (): UserInfo | undefined => {
  let userInfo: any = localStorage.getItem(USER_INFO) || undefined
  userInfo && (userInfo = JSON.parse(userInfo))
  return userInfo || {}
}

export const setLocalUserInfo = (userInfo: UserInfo): void => {
  localStorage.setItem(USER_INFO, userInfo ? JSON.stringify(userInfo) : '')
}

export const clearLocalUserInfo = (): void => {
  localStorage.removeItem(USER_INFO)
}

export const setLocalLoginStatus = (state: string): void => {
  localStorage.setItem(LOGIN_STATUS, state)
}

export const getLocalLoginStatus = (): string | undefined => {
  return localStorage.getItem(LOGIN_STATUS) || undefined
}
export const clearLocalLoginStatus = (): void => {
  localStorage.removeItem(LOGIN_STATUS)
}
// 获取消息
export const getNewsInfo = (): any => {
  let news: any = localStorage.getItem(NEWS_INFO)
  news = news ? JSON.parse(news) : {}
  return news
}

// 设置消息
export const setNewsInfo = (data: any): void => {
  localStorage.setItem(NEWS_INFO, JSON.stringify(data))
}

// 清除消息
export const clearNewsInfo = (): void => {
  localStorage.removeItem(NEWS_INFO)
}

// 获取配置信息
export const getSystemConfig = (): any => {
  const config: any = localStorage.getItem(SYSTEM_CONFIG)
  let finalResult: any = {}
  if (config) {
    finalResult = JSON.parse(config)
  }
  return finalResult
}

export const setSystemConfig = (data: any): void => {
  data = data ? JSON.stringify(data) : ''
  data && localStorage.setItem(SYSTEM_CONFIG, data)
}

export const clearSystemConfig = (): void => {
  localStorage.removeItem(SYSTEM_CONFIG)
}
